package com.classic.mafiacards;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

/**
 * Created by pavilion on 11/26/13.
 */
public class TimerActivity extends Activity {

    AnalogTimer myAnalogTimer;
    TextView myDigitalTimer;
    Vibrator vibrator;

    int i;

    private Handler myHandler = new Handler();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer);
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        myAnalogTimer = (AnalogTimer) findViewById(R.id.analogTimer);
        myDigitalTimer = (TextView) findViewById(R.id.digitalTimer);

        TimerValues.press_times = 0;
        TimerValues.timeSwap = 0;
        TimerValues.finalTime = 0;

        myDigitalTimer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TimerValues.press_times % 2 == 0) {
                    TimerValues.startTime = SystemClock.uptimeMillis();
                    myHandler.postDelayed(updateTimerMethod, 0);
                } else {
                    TimerValues.timeSwap += TimerValues.timeInMillies;
                    myHandler.removeCallbacks(updateTimerMethod);
                }
                TimerValues.press_times++;
                i++;
                Handler handler = new Handler();
                Runnable r = new Runnable() {

                    public void run() {
                        i = 0;
                    }
                };
                if (i == 1) {
                    handler.postDelayed(r, 250);
                } else if (i == 2) {
                    i = 0;
                    startActivity(new Intent(TimerActivity.this, TimerActivity.class));
                    finish();
                }
            }
        });

    }

    public void onPause() {
        super.onPause();
        if (TimerValues.press_times % 2 == 1) {
            TimerValues.timeSwap += TimerValues.timeInMillies;
        }
        myHandler.removeCallbacks(updateTimerMethod);
    }

    public void onResume() {
        super.onResume();
        if (TimerValues.press_times % 2 == 1) {
            TimerValues.press_times++;
            myDigitalTimer.performClick();
        } else {
            myDigitalTimer.setText(String.format("%02d", TimerValues.finalTime / 1000) + "    "
                    + String.format("%02d", (TimerValues.finalTime % 1000) / 10));
            myAnalogTimer.setTime(TimerValues.finalTime / 1000.0f);
        }
    }

    private Runnable updateTimerMethod = new Runnable() {

        public void run() {
            TimerValues.timeInMillies = SystemClock.uptimeMillis() - TimerValues.startTime;
            TimerValues.finalTime = TimerValues.timeSwap + TimerValues.timeInMillies;
            int seconds = (int) (TimerValues.finalTime / 1000) % 60;
            int milliseconds = (int) (TimerValues.finalTime % 1000);
            myAnalogTimer.setTime(TimerValues.finalTime / 1000.0f);
            myDigitalTimer.setText(String.format("%02d", seconds) + "    "
                    + String.format("%02d", milliseconds / 10));
            myHandler.postDelayed(this, 0);
            if(TimerValues.finalTime / 100 == 599) {
                TimerValues.press_times++;
                TimerValues.timeSwap = 0;
                TimerValues.finalTime = 0;
                myAnalogTimer.setTime(0);
                myDigitalTimer.setText("00    00");
                myHandler.removeCallbacks(updateTimerMethod);
                vibrator.vibrate(300);
            }
        }

    };

    public void onBackPressed() {
        if (CardsActivity.players.size() == 0) {

            startActivity(new Intent(TimerActivity.this, MainActivity.class).
                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
            myHandler.removeCallbacks(updateTimerMethod);

        } else {
            new AlertDialog.Builder(this)

                    .setTitle(getString(R.string.alert_close_game_title))

                    .setMessage(getString(R.string.alert_dilaog_close_message))

                    .setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int arg1) {
                            startActivity(new Intent(TimerActivity.this, MainActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                            android.os.Process.killProcess(android.os.Process.myPid());
                        }
                    })

                    .setNegativeButton(getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    })

                    .show();
        }
    }



    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_shuffle_cards, menu);
        getMenuInflater().inflate(R.menu.menu_open_manager, menu);
        if(CardsActivity.players.size() == 0)
            menu.getItem(1).setEnabled(false);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_shuffle_cards:
                this.onBackPressed();
                break;
            case R.id.action_open_manager:
                Intent intent = new Intent(this, ManagerActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);
                break;
        }
        return true;
    }
}
